package com.xiaogao.oss.controller;

import com.xiaogao.commonUtils.Result;
import com.xiaogao.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.HtmlUtils;

import java.net.URLDecoder;

/**
 * @author 小高
 * @createtime 2021-06-05-23:14
 * @description
 */
@Api(description="阿里云文件管理")
@RestController
@RequestMapping("/eduoss/file")
//@CrossOrigin // 跨域
public class OssController {

    @Autowired
    OssService ossService;

    /**
     * 用户头像上传至阿里云oss
     * @param file
     * @return
     */
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload")
    public Result uploadOssFile(
            @ApiParam(name = "file",value = "文件",required = true)
            MultipartFile file){

        // 获取上传文件 MultipartFile
        // 返回上传到oss的路径
        String url = ossService.uploadFile(file);

        return Result.ok().message("文件上传成功").data("url",url);
    }

    /**
     * 删除远程oss封面
     * @param coverName
     * @return
     */
    @ApiOperation(value = "删除封面")
    @DeleteMapping("/removeCourseById/{coverName}")
    public Result removeOssFile(
            @ApiParam(name = "coverName",value = "封面路径",required = true)
            @PathVariable("coverName") String coverName){

        String cover = URLDecoder.decode(coverName);
        ossService.removeFile(cover);
        return Result.ok().message("删除成功");
    }
}
